﻿using Abp.Dependency;
using Abp.WebApi.Controllers;
using GN.Pay.Application.Dto.Output;
using GN.Pay.Utils;
using GN.Pay.GatewayWeb.Controllers.Results;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using GN.Pay.Application.GatewayApp;
using GN.Pay.Application.Dto.Input;
using GN.Pay.Application.GatewayApp.Handle;

namespace GN.Pay.GatewayWeb.Controllers
{
    /// <summary>
    /// 网关控制器
    /// </summary>
    public class GatewayController : PayControllerBase
    {
        public GatewayController()
        {
        }

        /// <summary>
        /// 网关请求
        /// </summary>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult Go()
        {
            var requestDictionary = PreParam.GetQueryDictionary(Request);
            return new PayJsonResult(MessageOutput(requestDictionary));
        }

        /// <summary>
        /// 消息输出
        /// </summary>
        /// <param name="requestDictionary"></param>
        /// <returns></returns>
        private ServiceMessageOutput MessageOutput(IDictionary<string, string> requestDictionary)
        {
            if (requestDictionary == null)
            {
                return ServiceMessageOutput.CreateErrorOutput(0, "无请求信息");
            }
            string serviceName;
            if (requestDictionary.TryGetValue("service", out serviceName) && !string.IsNullOrWhiteSpace(serviceName))
            {
                try
                {
                    var service = GatewayServiceInstaller.Instance.GetGatewayService(serviceName);
                    if (service == null)
                    {
                        return ServiceMessageOutput.CreateErrorOutput(0, service + " 为无效或不支持的服务");
                    }
                    var serviceInput = GatewayManager.RequestServiceInput(requestDictionary);
                    object paramObject;
                    if (!string.IsNullOrWhiteSpace(serviceInput.Params))
                    {
                        paramObject = serviceInput.Params.JsonToObject(service.InputType, true);
                    }
                    else
                    {
                        paramObject = Activator.CreateInstance(service.InputType);
                    }
                    var input = (IServiceInput)paramObject;
                    input.AppId = serviceInput.AppId;
                    input.MerchantId = serviceInput.MerchantId;
                    var result = service.Call((IServiceInput)input);
                    return GatewayManager.SuccessResponseOutput(serviceInput.AppId, result, true);
                }
                catch (Exception err)
                {
                    this.Logger.Error("请求出错", err);
                    return ServiceMessageOutput.CreateErrorOutput(0, err.Message);
                }
            }
            else
            {
                return ServiceMessageOutput.CreateErrorOutput(0, "请求必须提供 service 参数");
            }
        }
    }
}
